package cn.tedu.overhaul.management.controller;

import cn.tedu.overhaul.management.pojo.dto.OverhaulMaintainAddNewDTO;
import cn.tedu.overhaul.management.pojo.dto.OverhaulMaintainUpdateDTO;
import cn.tedu.overhaul.management.pojo.vo.MaintainStandardNameVO;
import cn.tedu.overhaul.management.pojo.vo.MaintainVO;
import cn.tedu.overhaul.management.pojo.vo.ToolNameVO;
import cn.tedu.overhaul.management.service.IOverhaulService;
import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/overhaul")
@Api(tags = "01. 检查管理模块")
@Validated
public class OverhaulController {

    @Autowired
    IOverhaulService iOverhaulService;


    @PostMapping("/add-new")
    @ApiOperation("添加检查信息")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@ModelAttribute @Valid OverhaulMaintainAddNewDTO overhaulMaintainAddNewDTO) {
        log.debug("开始处理[添加检查信息] 参数:{}", overhaulMaintainAddNewDTO);
        iOverhaulService.addNew(overhaulMaintainAddNewDTO);
        return JsonResult.ok();
    }


    @GetMapping("/select_tool_number")
    @ApiOperation("查询工具所有编号")
    @ApiOperationSupport(order = 200)
    public JsonResult<List<ToolNameVO>> selectToolNumber() {
        List<ToolNameVO> list = iOverhaulService.selectToolNumber();
        return JsonResult.ok(list);
    }


    @GetMapping("")
    @ApiOperation("查询检修记录表")
    @ApiOperationSupport(order = 300)
    public JsonResult<PageData<MaintainVO>> selectMaintain(@Range(min=1,message="请提交有效的页码值!")Integer page, String queryType){
        log.debug("开始处理【查询检修记录表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<MaintainVO> pageData;
        if ("all".equals(queryType)) {
            pageData =iOverhaulService.selectMaintain(pageNum, Integer.MAX_VALUE);
        }else{
            pageData =iOverhaulService.selectMaintain(pageNum);
        }

        return JsonResult.ok(pageData);
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("修改面板查询检修记录")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('/overhaul/update')")
    public JsonResult<MaintainStandardNameVO> getStandardById(@PathVariable @Range(min = 1, message = "根据id修改面板查询检修记录失败，请提交合法的ID值！") Long id){
        log.debug("开始处理【根据ID修改面板查询检修记录】的请求，参数：{}", id);
        MaintainStandardNameVO queryResult=iOverhaulService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改检修记录")
    @ApiOperationSupport(order = 320)
    @PreAuthorize("hasAuthority('/overhaul/update')")
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "根据id修改检修记录失败，请提交合法的ID值！") Long id ,@Valid OverhaulMaintainUpdateDTO overhaulMaintainUpdateDTO){
        log.debug("开始处理【修改检修记录】的请求，ID：{}，新数据：{}", id, overhaulMaintainUpdateDTO);
        iOverhaulService.updateInfoById(id,overhaulMaintainUpdateDTO);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除检修记录")
    @PreAuthorize("hasAuthority('/overhaul/delete')")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的删除检修记录ID值！") Long id) {
        log.debug("开始处理【根据ID删除检修记录】的请求，参数：{}", id);
        iOverhaulService.delete(id);
        return JsonResult.ok();
    }
}
